package com.linln.admin.SpringBoot.controller;

import com.linln.admin.SpringBoot.domain.Lab;
import com.linln.admin.SpringBoot.domain.Repair;
import com.linln.admin.SpringBoot.service.DeviceService;
import com.linln.admin.SpringBoot.service.LabService;
import com.linln.admin.SpringBoot.service.RepairService;
import com.linln.admin.SpringBoot.validator.RepairValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Controller
@RequestMapping("/SpringBoot/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private LabService labService;
    @Autowired
    private UserService userService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("SpringBoot:repair:index")
    public String index(Model model, Repair repair) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();

        // 获取数据列表
        Example<Repair> example = Example.of(repair, matcher);
        Page<Repair> list = repairService.getPageList(example);
        List<Repair> content = list.getContent();
        for(Repair repair1 : content){
            repair1.setLabName(labService.getById(repair1.getLab_id()).getName());
            repair1.setPromoterName(userService.getById(repair1.getPromoter_id()).getNickname());
            repair1.setDeviceName(deviceService.getById(Long.parseLong(repair1.getDevice_id())).getName());
        }
        List<Lab> all = labService.findAll();


        // 封装数据
        model.addAttribute("list", content);
        model.addAttribute("all", all);

        model.addAttribute("page", list);
        return "/SpringBoot/repair/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("SpringBoot:repair:add")
    public String toAdd(Model model) {
        List<Lab> labs = labService.findAll();
        model.addAttribute("labs",labs);
        return "/SpringBoot/repair/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("SpringBoot:repair:edit")
    public String toEdit(@PathVariable("id") Repair repair, Model model) {
        model.addAttribute("repair", repair);
        List<Lab> labs = labService.findAlll();
        model.addAttribute("labs",labs);
        return "/SpringBoot/repair/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"SpringBoot:repair:add", "SpringBoot:repair:edit"})
    @ResponseBody
    public ResultVo save(@Validated RepairValid valid, Repair repair) {
        // 复制保留无需修改的数据
        if (repair.getId() != null) {
            Repair beRepair = repairService.getById(repair.getId());
            EntityBeanUtil.copyProperties(beRepair, repair);
        }

        // 保存数据
        repair.setEnd((byte)0);
        repairService.save(repair);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("SpringBoot:repair:detail")
    public String toDetail(@PathVariable("id") Repair repair, Model model) {
        model.addAttribute("repair",repair);
        return "/SpringBoot/repair/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("SpringBoot:repair:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (repairService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}